package com.xiaotu.spider.iqy.pageprocessor;

import com.xiaotu.common.exception.SpiderException;
import com.xiaotu.common.exception.SpiderExceptionCode;
import com.xiaotu.common.util.*;
import com.xiaotu.spider.AUrlPageProcessor;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.Page;

import java.util.List;
import java.util.Map;

/**
 * 爱奇艺短评数据爬取
 *
 * @auther GaoHJ
 * @create 2017/11/3
 */
@Component("IQYContentPageProcessor")
public class IQYContentPageProcessor extends AUrlPageProcessor
{

    public static final String CONTENT_URL = "http://api-t.iqiyi" +
            ".com/feed/get_feeds?callback=jQuery07450437664794147_%s&authcookie" +
            "=&device_id=pc_web&m_device_id=ba0d7cfee4a08e09ab9fb7a9dfdd645f&agenttype=118&wallId" +
            "=209304147&feedTypes=1%%2C7&count=20&top=1&hasRecomFeed=1&feedId=%s" +
            "&needTotal=1&version=1&qypid=01010011010000000000&isVideoPage=true&tvid=%s" +
            "&categoryid=2&albumid=%s&upOrDown=%s&snsTime=%s&_=%s";

    @Override
    public void process(Page page)
    {
        if (Constants.PageDataType.TV_RECORD.equals(this.getPageDataType(page)))
            this.pageProcess(page);
        else
            this.commentProcess(page);
    }

    protected String getDataId(Page page)
    {
        String dataId = RegexUtils.regexSearch("albumId:(" + RegexUtils.REGEX_INTEGER + ")", page
                .getRawText());
        return RegexUtils.regexSearch(RegexUtils.REGEX_INTEGER, dataId);
    }

    private void pageProcess(Page page)
    {
        String dataId = this.getDataId(page);
        if (StringUtils.isEmpty(dataId))
            throw new SpiderException(SpiderExceptionCode.DataInvalideException);
        List<String> tvIdList = page.getHtml().xpath("//ul[@class='juji-list  " +
                "clearfix']/li[@data-videolist-tvid]/@data-videolist-tvid").all();
        if (tvIdList == null || tvIdList.isEmpty())
            throw new SpiderException(SpiderExceptionCode.DataInvalideException);
        long time = System.currentTimeMillis();
        tvIdList.stream().limit(1).forEach(tvId -> PageUtils.addTargetRequest(page, String.format(CONTENT_URL,
                time, StringUtils.EMPTY, tvId, dataId, "0", "1", time), PageUtils
                .getNewExtraMapFromPage(page, new String[]{"dataId", "tvId", "time"}, new
                        Object[]{dataId, tvId, time})));
    }

    private void commentProcess(Page page)
    {
        String json = page.getRawText();
        if (StringUtils.isEmpty(json))
            throw new SpiderException(SpiderExceptionCode.DataInvalideException);
        json = json.substring(json.indexOf("({") + 1, json.lastIndexOf(")}catch"));
        Map<String, Object> resMap = GsonUtils.fromJson(json, Map.class);
        Map<String, Object> dataMap = (Map<String, Object>) resMap.get("data");
        if (dataMap == null)
            throw new SpiderException(SpiderExceptionCode.DataInvalideException);
        List<Map<String, Object>> feedList = (List<Map<String, Object>>) dataMap.get("feeds");
        if (feedList == null || feedList.isEmpty())
            throw new SpiderException(SpiderExceptionCode.DataInvalideException);

        long feedId = 0;
        long snsTime = 0;
        for (Map<String, Object> feedMap : feedList)
        {
            feedId = MathUtil.toBigDecimal(feedMap.get("feedId")).longValue();
            snsTime = MathUtil.toBigDecimal(feedMap.get("snsTime")).longValue();
            String user = StringUtil.nullToString(feedMap.get("name"));
            String date = DateUtils.transferLongToDateString(DateUtils.DAY_FORMAT, MathUtil
                    .toBigDecimal(feedMap.get("releaseDate")).longValue() * 1000);
            String description = StringUtil.nullToString(feedMap.get("description"));
            if (StringUtils.isEmpty(description))
                continue;
            FileUtil.writeFile("D://content.csv", user + SepratorUtil.SEP_PIPE + date +
                    SepratorUtil.SEP_PIPE + description + SepratorUtil.LINE_SEPRATOR);
        }

        int totalCount = MathUtil.toBigDecimal(dataMap.get("totalCount")).intValue();
        long time = MathUtil.toBigDecimal(page.getRequest().getExtra("time")).longValue();
        if (page.getRequest().getExtra("pageSize") == null)
        {
            PageUtils.addTargetRequest(page, String.format(CONTENT_URL, time, feedId, page
                    .getRequest().getExtra("tvId"), page.getRequest().getExtra("datId"), "1",
                    snsTime, time + 1), PageUtils.getNewExtraMapFromPage(page, new
                    String[]{"pageSize", "pageNo"}, new Object[]{totalCount % 20 == 0 ?
                    totalCount / 20 : totalCount / 20 + 1, 2}));
            return;
        }

        int pageSize = (Integer) page.getRequest().getExtra("pageSize");
        int pageNo = (Integer) page.getRequest().getExtra("pageNo");
        if (pageNo >= pageSize)
            return;
        PageUtils.addTargetRequest(page, String.format(CONTENT_URL, time, feedId, page.getRequest
                        ().getExtra("tvId"), page.getRequest().getExtra("datId"), "1", snsTime,
                time + 1)
                , PageUtils.getNewExtraMapFromPage(page, new String[]{"pageSize", "pageNo"}, new
                        Object[]{pageSize, pageNo + 1}));

    }

    @Override
    protected String[] getStartRequestUrls()
    {
        return new String[]{"http://www.iqiyi.com/v_19rrg37vqc.html?vfm=2008_aldbd"};
    }

    @Override
    protected String getSpiderType()
    {
        return "IQY_Content";
    }

    @Override
    public String getPageDataType(Page page)
    {
        if (page.getUrl().toString().contains("http://api-t.iqiyi.com/feed/get_feeds"))
            return Constants.PageDataType.TV_COMMENT;
        return Constants.PageDataType.TV_RECORD;
    }
}
